import {
	wxPay,
	baiduPay,
	alipay
} from "../utils/weixinPay";

export const balance = {

	methods: {
		Addrecharge(amount, donation_amount, integral, remark, status = 0) {

			return new Promise((resolve, reject) => {
				let params = {
					order_no: '', // 自动生成
					trade_no: '',
					order_id: 0,
					type: 2,
					pay_type: 1,
					amount: amount + (donation_amount || 0),
					remark: remark || '在线充值',
					order_type: 0,
					status: status,
					integral: integral || 0
				}
				this.$resPost('AddUsersConsume', {
					...params
				}).then(res => {
					if (status == 1) {
						resolve(res.data)
					} else {
						// #ifdef MP-WEIXIN || H5 || APP-PLUS
						wxPay({
								order_no: res.data,
								total_fee: amount
							})
							.then(res1 => {
								this.$resPost('UpdateConsumeStatus', {
									order_no: res.data,
									status: 1
								}).then(res2 => {
									resolve(res.data)
								}).catch(() => {
									reject()
								})
							})
							.catch(() => {
								reject()
							});
						// #endif
						// #ifdef MP-BAIDU
						baiduPay({
								order_no: res.data,
								total_fee: amount
							}).then(res1 => {
								this.$resPost('UpdateConsumeStatus', {
									order_no: res.data,
									status: 1
								}).then(res2 => {
									resolve(res.data)
								}).catch(() => {
									reject()
								})
							})
							.catch(() => {
								reject()
							});
						// #endif
						// #ifdef MP-ALIPAY
						alipay({
								order_no: res.data,
								total_fee: amount
							})
							.then(res1 => {
								this.$resPost('UpdateConsumeStatus', {
									order_no: res.data,
									status: 1
								}).then(res2 => {
									resolve(res.data)
								}).catch(() => {
									reject()
								})
							})
							.catch(() => {
								reject()
							});
						// #endif
					}
				}).catch(() => {
					reject()
				})
			})
		},

		// 短信充值
		addSMSOnlineRecharge(paramsObj) {
			return new Promise((resolve, reject) => {
				
				let params = {
					business_id: this.business_id,
					status: 0,
					num:paramsObj.num,
					price: paramsObj.amount,
					remark: this.vuex_user.pk_id
				}
				let order_no;
				this.$resPost('SMSOnlineRecharge', params).then(res => {
					order_no = res.data;
					wxPay({
							order_no: res.data,
							total_fee: paramsObj.amount
						})
						.then(res => {
							this.$resPost('UpdateSmsRechargeStatus', {
								order_no: order_no
							}).then(res => {
								resolve({
									orderNo: order_no,
									amount:paramsObj.amount,
								})
							}).catch((err1) => {
								console.log(err1,'err2')
								reject()
							})
						})
						.catch((err2) => {
							console.log(err2,'err2')
							reject()
						});
				})
			})
		}
	}

}
